//
//  main.m
//  04-NSDictionaryHome
//
//  Created by 曹曹 on 16/7/7.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Color.h"

void homeWork () {
    NSString *path = @"/Users/caocao/DLS160710/Objective-C/HomeWork/04-NSDictionaryHome/04-NSDictionaryHome/crayons.txt";
    // 通过文件路径, 读取文件中的字符串
    NSString *sourceString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"----%@", sourceString);
    // 根据 \n 切割字符串
    NSArray *colorStringArray = [sourceString componentsSeparatedByString:@"\n"];
    NSLog(@"----%@", colorStringArray);
    // 声明存储颜色的可变字典
    NSMutableDictionary *colorDictionary = [NSMutableDictionary dictionary];
    for (int i = 0; i < colorStringArray.count; i++) {
        // 筛掉空的字符串!!!!!!!!!!!!!!!!!
        if ([colorStringArray[i] length] > 0) {
            NSArray *color = [colorStringArray[i] componentsSeparatedByString:@" #"];
            [colorDictionary setObject:color.lastObject forKey:color.firstObject];
        }
    }
    NSLog(@"%s %@", __func__, colorDictionary);
#pragma mark - 第2步
    NSMutableDictionary *newColorDictionary = [NSMutableDictionary dictionary];
    NSArray *allKeys = colorDictionary.allKeys;
    for (int i = 0; i < allKeys.count; i++) {
        NSString *groupName = [allKeys[i] substringWithRange:NSMakeRange(0, 1)];
        NSMutableArray *colorArray = [newColorDictionary objectForKey:groupName];
        Color *newColor = [[Color alloc] initWithName:allKeys[i]
                                           colorValue:[colorDictionary objectForKey:allKeys[i]]];
        if (colorArray == nil) {
            colorArray = [NSMutableArray arrayWithObject:newColor];
            [newColorDictionary setObject:colorArray forKey:groupName];
        } else {
            [colorArray addObject:newColor];
        }
    }
    NSLog(@"%@", newColorDictionary);
}

int main(int argc, const char * argv[]) {
#if 1
    //处理⽂件crayons.txt中的⽂本信息，⽂本内容是关于颜⾊的，每⾏都是⼀ 个颜⾊的信息，例如：Almond #EED9C4，前⼀个字符串是颜⾊的名称，后⼀ 个字符串是颜⾊的16进制⾊值，处理⽂本完成如下需求:
    //使⽤字典管理所有的颜⾊，即字典中存储的是多个键值对，颜⾊名称为 key，16进制颜⾊值（不带#）是value。
    //使⽤⼀个新的字典管理颜⾊，对颜⾊进⾏分类管理，即：“A”，“B”，“C”… 即这个字典包含多个键值对，key是26个字⺟，value是数组，数组⾥存放的是 Color对象（包含name和colorValue）。需要⾃⼰创建Color类。
    NSString * dataString = [NSString stringWithContentsOfFile:@"/Users/caocao/DLS160710/Objective-C/HomeWork/04-NSDictionaryHome/04-NSDictionaryHome/crayons.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *array = [dataString componentsSeparatedByString:@"\n"];
    // 使用字典管理所有的颜色
    NSMutableDictionary *colorDic = [NSMutableDictionary dictionary];
    for (NSString *color in array) {
        if ([color length] > 0) {
            NSArray *colorFull = [color componentsSeparatedByString:@" #"];
            [colorDic setObject:colorFull.lastObject forKey:colorFull.firstObject];
        }
    }
    NSLog(@"%@", colorDic);
    // 对颜色进行分类管理
    NSMutableDictionary *colorManager = [NSMutableDictionary dictionary];
    for (int i = 0; i < colorDic.count; i++) {
        Color *color = [[Color alloc] initWithName:colorDic.allKeys[i] colorValue:colorDic.allValues[i]];
            NSString *group = [color.name substringWithRange:NSMakeRange(0, 1)];
            NSMutableArray *colorArr = [colorManager objectForKey:group];
            if (colorArr == nil) {
                colorArr = [NSMutableArray arrayWithObject:color];
                [colorManager setObject:colorArr forKey:group];
            } else {
                [colorArr addObject:color];
            }
    }
    NSLog(@"%@", colorManager);
#endif
    homeWork();
    return 0;
}
